package com.xinwei.controller;

import com.auth0.jwt.JWT;
import com.xinwei.common.ApiRestResponse;
import com.xinwei.entity.User;
import com.xinwei.excpetion.ManagerExceptionEnmu;
import com.xinwei.service.UserService;
import com.xinwei.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ApiRestResponse login(@RequestBody User user){
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword())){
            return ApiRestResponse.error(ManagerExceptionEnmu.NEED_PASSWORD_AND_USERNAME);
        }
        userService.login(user);
        String token = JWTUtils.getToken();
        return ApiRestResponse.success(token );
    }

}
